angular.module('BaseServer')

.service('BaseClass', ['BaseEnum', function(BaseEnum){
    return {
        Round: function(precision, roundType, roundValue){
            this.Precision = 0;
            this.RoundType = BaseEnum.RoundType.AccondValueRound.Value;
            this.RoundValue = 5;

            if (precision != undefined) {
                this.Precision = precision;
            }
            if (roundType != undefined) {
                this.RoundType = roundType;
            }
            if (roundValue != undefined) {
                this.RoundValue = roundValue;
            }

            this.formatNumber = function(val) {
                var returnValue;

                if(val == undefined || val == null || val.toString().trim() == ''){
                    return '';
                }
                if (this.RoundType == BaseEnum.RoundType.AllAdd.Value) {
                    var value = parseInt(val * Math.pow(10, this.Precision + 1));

                    if (value % 10 > 0) {
                        returnValue = parseInt(val * Math.pow(10, this.Precision)) + 1;
                    }
                    else{
                        returnValue = parseInt(val * Math.pow(10, this.Precision))
                    }

                    returnValue = returnValue / Math.pow(10, this.Precision);
                }
                else if (this.RoundType == BaseEnum.RoundType.AllDelete.Value) {
                    returnValue = parseInt(val * Math.pow(10, this.Precision)) / Math.pow(10, this.Precision);
                }
                else {
                    returnValue = parseInt(val * Math.pow(10, this.Precision + 1));

                    if (returnValue % 10 >= this.RoundValue) {
                        returnValue = parseInt(val * Math.pow(10, this.Precision)) + 1;
                    }
                    else {
                        returnValue = parseInt(returnValue / 10);
                    }
                    returnValue = returnValue / Math.pow(10, this.Precision);
                }

                return parseFloat(returnValue).toFixed(this.Precision);
            };

            this.formatPercent = function(val){
                if(val == undefined || val == null || val.toString().trim() == ''){
                    return '';
                }

                return this.formatNumber(val * 100).toString() + "%";
            };
        },
        Event: function(){
            this._listeners = {};

            this.addListener = function(eventName, fun){
                eventName = eventName.trim().toLowerCase();
                if(!this._listeners[eventName]){
                    this._listeners[eventName] = [];
                }
                this._listeners[eventName].push(fun);
            };
            this.removeListener = function(eventName, fun){
                if(fun == null){
                    delete this._listeners[eventName];
                }
                else{
                    for(var i=0; i<this._listeners[eventName].length; i++){
                        if(this._listeners[eventName][i] == fun){
                            this._listeners[eventName].splice(i, 1);
                        }
                    }
                }
            };
            this.dispatchEvent = function(eventName){
                eventName = eventName.trim().toLowerCase();
                var _listeners = this._listeners[eventName];

                for(var i=0; i<this._listeners[eventName].length; i++){
                    this._listeners[eventName][i].apply();
                }
            };
        }
    };
}]);

